//
//  UploadImageManager.m
//  fashionbuyer
//
//  Created by ale tan on 2018/1/29.
//  Copyright © 2018年 ale tan. All rights reserved.
//

#import "UploadImageManager.h"
#import <Photos/PHImageManager.h>

#import "UploadImageRequest.h"
#import "MBManager.h"

@implementation UploadImageManager

- (void)chooseImagesWithType:(ImagePickerType)type DidChooseImage:(DidChooseImage)didChooseImage{
    TZImagePickerController *imagePickerVC = [[TZImagePickerController alloc] initWithMaxImagesCount:_allowPickMutiple?9:1 delegate:nil];
    imagePickerVC.sortAscendingByModificationDate = NO;
    imagePickerVC.allowPickingOriginalPhoto = NO;
    imagePickerVC.allowPreview = NO;
//    imagePickerVC.oKButtonTitleColorNormal = RGB_RED_COLOR;
    //选择视频
    if (type == ImagePickerTypeVideo) {
        imagePickerVC.allowPickingImage = NO;
        imagePickerVC.allowPickingVideo = YES;
    }
    //选择照片
    else{
        imagePickerVC.allowPickingImage = YES;
        imagePickerVC.allowPickingVideo = NO;
    }
    
    [imagePickerVC setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [MBManager showLoading];
        });
        
        NSMutableArray *imageArray = [NSMutableArray array];
        for (PHAsset *asset in assets) {
            PHImageRequestOptions *phImageRequestOptions = [[PHImageRequestOptions alloc] init];
            phImageRequestOptions.resizeMode = PHImageRequestOptionsResizeModeFast;
            phImageRequestOptions.synchronous = YES;
            phImageRequestOptions.deliveryMode = PHImageRequestOptionsDeliveryModeFastFormat;
            
            [[PHImageManager defaultManager] requestImageDataForAsset:asset options:phImageRequestOptions resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
                
                UIImage *image = [UIImage imageWithData:imageData];
//                long long size = imageData.length/1000;
                [imageArray addObject:image];
                
                if (imageArray.count == assets.count) {
                    
                    dispatch_async(dispatch_get_main_queue(), ^{
                        [MBManager hideAlert];
                        
                        if (didChooseImage) {
                            didChooseImage(imageArray);
                        }
                    });
                }
            }];
        }
    }];
    
    UIViewController *rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
    [rootViewController presentViewController:imagePickerVC animated:YES completion:nil];
}

- (void)uploadImages:(NSArray <UIImage *>*)images DidUploadImage:(DidUploadImage)didUploadImage{
    
    NSMutableArray *imageUrls = [NSMutableArray array];
    
    __block NSInteger callBackCount = 0;
    for (UIImage *image in images) {
        UploadImageRequest *request = [UploadImageRequest new];
        [request upLoadImage:image Callback:^(id responseDic, BOOL result) {
            if (result) {
                [imageUrls addObject:responseDic];
            }
            
            callBackCount ++;
            
            if (callBackCount == images.count) {
                
                if (didUploadImage) {
                    didUploadImage(imageUrls);
                }
            }
        }];
    }
}

@end
